import { createAsyncThunk,createSlice } from "@reduxjs/toolkit";
import axios from 'axios'

const initialState={
    count:0,
    price:10.5,
    sum:0,
    channelList:[]
}

const fetchChannels = createAsyncThunk('counter/fetchChannels',async()=>{
    let res = await axios.get('http://blogapi.shbwyz.com/article/list')
    return res.data.rows
})

const counterSilce = createSlice({
    name: 'counter',
    initialState, 
    reducers: { 
      increment(state, { payload }) {
        state.count += payload
        state.sum = state.count * state.price
      },
      decrement(state) {
        state.count -= 1
      }
    },
  
    extraReducers: (builder) => {
      builder
        .addCase(fetchChannels.pending, (state) => {
        })
        .addCase(fetchChannels.fulfilled, (state, { payload }) => {
          console.log(payload, 'payload')
          state.channelList = payload
        })
        .addCase(fetchChannels.rejected, (state) => {
          console.log(state, '请求失败')
        })
    }
  
  })
  
  export { fetchChannels }
  export const { increment, decrement, setChannerlList } = counterSilce.actions
  export default counterSilce.reducer
